/**
 * 
 */
package com.kunbo.cn.controller.core.inventory;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.kunbo.cn.authority.RequiredPermission;
import com.kunbo.cn.entity.core.inventory.Organization;
import com.kunbo.cn.entity.system.Depart;
import com.kunbo.cn.service.core.inventory.OrganizationService;
import com.kunbo.cn.service.system.DepartService;
import com.kunbo.cn.util.json.Json;
import com.kunbo.cn.util.retu.CommonResult;
import com.kunbo.cn.util.retu.ReturnBean;
import com.kunbo.cn.util.string.QString;

/**
 *
 * @author bjl  
 * @date 2019年2月28日 
 */

@RestController  
@RequestMapping("/Organization/*")  
public class OrganizationController {
	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
	@Autowired
    private JdbcTemplate jdbcTemplate;

	@Autowired
	private OrganizationService Service;
	
	@GetMapping("/getTreeList")
	public List getTreeList() {
		List list = Service.getOrganizationTreeList();
		return list;
	}
	
	//组织新增
	@PostMapping("/add")
	public CommonResult addOrganization(Organization organize) {
		Service.save(organize);
		return new CommonResult(true,"操作成功",null);
	}
	/**
	 * 生成库存编码
	 * 
	 */
	@GetMapping("/createCode")
	public String createCode(){
		 String prex = "KCD";
		 String suffix="1001";
		 int n=0;
			String sql="";
			sql +="select\r\n" ; 
			sql +=" MAX(autonomy) as au\r\n";
			sql +="FROM business_inventory_manage d\r\n";
			List<Map<String,Object>> list = jdbcTemplate.queryForList(sql);
			Integer t = (Integer) list.get(0).get("au");
			//Float lowerAmount=t-t1;
			String inventoryCode="";
			String time =sdf.format(new Date());
			if(t==null){
				System.out.println("***********tare**********");
				inventoryCode=prex+time+suffix;
			}else {
				int f=t+1;
				inventoryCode=prex+time+f;
			}
		return inventoryCode;
	}
	/**
	 *修改状态 
	 * 
	 */
	/*@PutMapping("/updateState")
	public CommonResult updateValid(Organization organize) {
		service.updateState(organize);
		return new CommonResult(true,"操作成功",null);
	}*/
	
	@PutMapping("/update")
	public CommonResult updateOrganization(Organization organize) {
		Service.update(organize);
		return new CommonResult(true,"操作成功",null);
	}
	
	@GetMapping("/findById")
	public Organization findById(String id) {
		Organization d = Service.findById(id);
		return d;
	}
	@DeleteMapping("/delete")
	public CommonResult delete(String id) {
		 Service.delete(id);
		return new CommonResult(true,"操作成功",null);
	}
	/*@PutMapping("/updateValid")
	public CommonResult updateValid(Organization organize) {
		Service.updateValidState(organize);
		return new CommonResult(true,"操作成功",null);
	}*/
	//条件分页查询组织机构
	@GetMapping("/findPageByTitle")
	public Page<Organization> findOrganizationByTitle(String title,Integer pageNo,Integer pageSize) {
		Page<Organization> page = Service.findOrganizationByTitle(title, pageNo, pageSize);
		return page;
	}
}
